package com.rd.student.jike.class2.lihongming.week724_730.Day728;

import com.rd.student.dashuju.zhengruixian.Thread1;

import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Auther:Heinrich
 * @Date:2023/7/28-07-28-9:00
 * @Description:com.rd.student.jike.class2.lihongming.exercise3
 * @version:1.0
 */
public class Question6 {
    public static void main(String[] args) {
        System.out.println("请输入15整数倍的数字");
        Scanner scanner = new Scanner(System.in);
        NumPrint numPrint = new NumPrint(1,scanner.nextInt());
        Thread t1 = new Thread(new RunnableNum1(numPrint));
        Thread t2 = new Thread(new RunnableNum2(numPrint));
        Thread t3 = new Thread(new RunnableNum3(numPrint));
        t1.start();
        t2.start();
        t3.start();
    }


}
class RunnableNum1 implements Runnable{
    NumPrint print;

    public RunnableNum1(NumPrint print) {
        this.print = print;
    }

    @Override
    public void run() {
        print.printNum1();
    }
}
class RunnableNum2 implements Runnable{
    NumPrint print;

    public RunnableNum2(NumPrint print) {
        this.print = print;
    }

    @Override
    public void run() {
        print.printNum2();
    }
}

class RunnableNum3 implements Runnable{
    NumPrint print;

    public RunnableNum3(NumPrint print) {
        this.print = print;
    }

    @Override
    public void run() {
        print.printNum3();
    }
}
class NumPrint{
    private AtomicInteger num = new AtomicInteger(1);
    public int temp;//1,2,3
    public int amount;

    public NumPrint(int temp,int amount) {
        this.temp = temp;
        this.amount = amount;
    }

    public synchronized void printNum1() {
        for (int i = 0; i < (int) (amount / 15); i++) {
            while (temp != 1) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            for (int j = 0; j < 5; j++) {
                System.out.println(Thread.currentThread().getName()+" "+num.getAndIncrement());
            }
            temp = 2;
            this.notifyAll();
        }
    }

    public synchronized void printNum2() {
        for (int i = 0; i < (int) (amount / 15); i++) {
            while (temp != 2) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            for (int j = 0; j < 5; j++) {
                System.out.println(Thread.currentThread().getName()+" "+num.getAndIncrement());
            }
            temp = 3;
            this.notifyAll();
        }
    }

    public synchronized void printNum3() {
        for (int i = 0; i < (int) (amount / 15); i++) {
            while (temp != 3) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            for (int j = 0; j < 5; j++) {
                System.out.println(Thread.currentThread().getName()+" "+num.getAndIncrement());
            }
            temp = 1;
            this.notifyAll();
        }
    }
}